var gl_bed_vue_vm_list_html = $('#bed_vue_vm_list').html();

$(function() {
	$('#bed_vue_vm_list div').hide();
	
	load_subjectRoom_select();
	load_houseType_select_by_subjectRoomId();
	load_building_select_by_houseTypeId();
});

function load_subjectRoom_select() {
	if($('#subjectRoom_select').length==0) {return;}

	$('#subjectRoom_select').html('<option value="" selected></option>');
	$('#houseType_select').html('<option value="" selected></option>');
	$('#building_select').html('<option value="" selected></option>');
	$('#floor_select').html('<option value="" selected></option>');

	findAll_basic(path+'/subjectRoom/findAll', function(response) {
		for(var i=0; i<response.data.length; i++) {
			$('#subjectRoom_select').append('<option value="' + response.data[i].subjectRoomId
				+ '">' + response.data[i].roomName + '</option>');
		}
		
		_addChangeListener("#subjectRoom_select", function() {
			var subjectRoomId = $('#subjectRoom_select').val();
			if(subjectRoomId!='') {
				var option = $('#subjectRoom_select option:selected');
				view.data.subjectRoomId = subjectRoomId;
				view.data.subjectRoomName = option.text();
			}
		});
	});
}

function load_houseType_select_by_subjectRoomId() {
	if($('#houseType_select').length==0) {return;}

	$('#houseType_select').html('<option value="" selected></option>');
	$('#building_select').html('<option value="" selected></option>');
	$('#floor_select').html('<option value="" selected></option>');

	find_basic_by_json(path+'/houseType/findAll', {}, function(response) {
		for(var i=0; i<response.data.length; i++) {
			$('#houseType_select').append('<option value="' + response.data[i].houseTypeId
				+ '">' + response.data[i].houseType + '</option>');
		}
		
		_addChangeListener("#houseType_select", function() {
			var houseTypeId = $('#houseType_select').val();
			if(houseTypeId!='') {
				var option = $('#houseType_select option:selected');
				view.data.houseTypeId = houseTypeId;
				view.data.houseTypeName = option.text();
			}
		});
	});
}

function load_building_select_by_houseTypeId() {
	if($('#building_select').length==0) {return;}

	$('#building_select').html('<option value="" selected></option>');
	$('#floor_select').html('<option value="" selected></option>');

	find_basic_by_json(path+'/building/findAll', {}, function(response) {
		for(var i=0; i<response.data.length; i++) {
			$('#building_select').append('<option value="' + response.data[i].buildingId
				+ '">' + response.data[i].buildingName + '</option>');
		}
		
		_addChangeListener("#building_select", function() {
			var buildingId = $('#building_select').val();
			if(buildingId!='') {
				load_floor_select_by_buildingId(buildingId);
				
				var option = $('#building_select option:selected');
				view.data.buildingId = buildingId;
				view.data.buildingName = option.text();
			}
		});
	});
}

function load_floor_select_by_buildingId(buildingId) {
	if($('#floor_select').length==0) {return;}

	$('#floor_select').html('<option value="" selected></option>');
	$('#bed_select').html('<option value="" selected></option>');

	find_basic_by_json(path+'/floor/find', {buildingId: buildingId}, function(response) {
		for(var i=0; i<response.data.length; i++) {
			$('#floor_select').append('<option value="' + response.data[i].floorId
				+ '">' + response.data[i].floorName + '</option>');
		}
		
		_addChangeListener("#floor_select", function() {
			var floorId = $('#floor_select').val();
			if(floorId!='') {
				var option = $('#floor_select option:selected');
				view.data.floorId = floorId;
				view.data.floorName = option.text();
				
				if(load_bed_by_floorId) {
					load_bed_by_floorId(floorId);
				}
			}
		});
	});
}

function deleteBatch() {
	deleteBatch_basic(path+'/elderBeHospital/deleteBatch');
}
